package com.mf.ergate.controller.auth;

import com.mf.ergate.api.auth.UserInfoRemoteService;
import com.mf.ergate.api.auth.model.dto.UserInfoCreateDto;
import com.mf.ergate.api.auth.model.dto.UserInfoDeleteDto;
import com.mf.ergate.api.auth.model.dto.UserInfoQueryDto;
import com.mf.ergate.api.auth.model.dto.UserInfoUpdateDto;
import com.mf.ergate.api.auth.model.vo.UserInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.auth.UserInfoEntity;
import com.mf.ergate.service.auth.UserInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 用户信息Controller
 * User: zhaoming
 * DateTime: 2018-10-10
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class UserInfoController implements UserInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 新增
     * @param userInfoCreateDto 用户信息新增Dto
     * @return 用户信息
     */
    public Result<UserInfoVo> insert(@RequestBody UserInfoCreateDto userInfoCreateDto) {
        UserInfoEntity userInfoEntity = MfBeanUtils.copyObject(userInfoCreateDto, UserInfoEntity.class);
        userInfoEntity = userInfoService.insert(userInfoEntity);
        UserInfoVo userInfoVo = MfBeanUtils.copyObject(userInfoEntity, UserInfoVo.class);
        return new Result<>(userInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param userInfoUpdateDto 用户信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody UserInfoUpdateDto userInfoUpdateDto) {
        UserInfoEntity userInfoEntity = MfBeanUtils.copyObject(userInfoUpdateDto, UserInfoEntity.class);
        Boolean flag = userInfoService.updateByUniqueNo(userInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param userInfoDeleteDto 用户信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody UserInfoDeleteDto userInfoDeleteDto){
        UserInfoEntity userInfoEntity = MfBeanUtils.copyObject(userInfoDeleteDto, UserInfoEntity.class);
        Boolean flag = userInfoService.deleteByUniqueNo(userInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param userNo 用户编号
     * @return 用户信息
     */
    public Result<UserInfoVo> selectByUniqueNo(@RequestBody String userNo) {
        UserInfoEntity userInfoEntity = userInfoService.selectByUniqueNo(userNo);
        UserInfoVo userInfoVo = MfBeanUtils.copyObject(userInfoEntity, UserInfoVo.class);
        return new Result<>(userInfoVo);
    }

    /**
     * 根据条件查询
     * @param userInfoQueryDto 用户信息查询Dto
     * @return 用户信息列表
     */
    public Result<List<UserInfoVo>> selectByCondition(@RequestBody UserInfoQueryDto userInfoQueryDto) {
        List<UserInfoEntity> userInfoEntities = userInfoService.selectByCondition(userInfoQueryDto);
        List<UserInfoVo> userInfoVos = MfBeanUtils.copyList(userInfoEntities, UserInfoVo.class);
        return new Result<>(userInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param userInfoQueryDto 用户信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody UserInfoQueryDto userInfoQueryDto) {
        return new Result<>(userInfoService.selectCount(userInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 用户信息分页列表
     */
    public Result<Page<UserInfoVo>> selectPage(@RequestBody RemotePage<UserInfoQueryDto> remotePage){
        Page<UserInfoEntity> userInfoEntityPage = userInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<UserInfoVo> page = MfBeanUtils.copyPage(userInfoEntityPage, UserInfoVo.class);
        return new Result<>(page);
    }

    /**
     * 删除用户及相关信息(逻辑删除)
     * @param userInfoDeleteDto 用户信息删除Dto
     * @return 执行结果
     */
    public Result<Boolean> deleteCompleteByUniqueNo(@RequestBody UserInfoDeleteDto userInfoDeleteDto){
        UserInfoEntity userInfoEntity = MfBeanUtils.copyObject(userInfoDeleteDto, UserInfoEntity.class);
        Boolean flag = userInfoService.deleteCompleteByUniqueNo(userInfoEntity);
        return new Result<>(flag);
    }

}